package com.huiztech.maystall.mvc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.huiztech.maystall.activity.HomeActivity;
import com.huiztech.maystall.activity.R;
import com.huiztech.maystall.http.AsyncHttpClient;
import com.huiztech.maystall.http.MyAsyncHttpResponseHandler;
import com.huiztech.maystall.http.RequestParams;

public class Controller {

    private MaystallView view = null;
    private Context context = null;

    public Controller(MaystallView view, Context context) {
        this.view = view;
        this.context = context;
    }

    /**
     * 
     * 注册
     * 
     * @param email
     * @param name
     * @param password
     * @param sex
     */
    public void doRegister(final String email, String name, final String password, String sex) {

        RequestParams params = new RequestParams();
        params.put("email", email);
        params.put("name", name);
        params.put("password", password);
        params.put("sex", sex);

        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(Consts.URL_REGISTER, params, new MyAsyncHttpResponseHandler(context, view) {

            @Override
            public void onSuccess(String json) {
                super.onSuccess(json);
                doLogin(email, password);
            }
        });
    }

    /**
     * 登录
     * 
     * @param username
     * @param password
     */
    public void doLogin(String username, String password) {

        RequestParams params = new RequestParams();
        params.put("username", username);
        params.put("password", password);

        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(Consts.URL_LOGIN, params, new MyAsyncHttpResponseHandler(context, view) {

            @Override
            public void onSuccess(String json) {
                super.onSuccess(json);
                Intent intent = new Intent(context, HomeActivity.class);
                context.startActivity(intent);
            }

        });
    }

    // 取得广告详细内容
    public List<Map<String, Object>> getAdDetail() {

        // AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        // asyncHttpClient.post(Consts.URL_ADDETAIL, new
        // MyAsyncHttpResponseHandler(context, view) {
        //
        // @Override
        // public void onSuccess(String json) {
        // super.onSuccess(json);
        //
        // }
        //
        // });

        int[] imageResId = new int[] { R.drawable.demo_home_ad1, R.drawable.demo_home_ad2, R.drawable.demo_home_ad3,
                R.drawable.demo_home_ad4 };
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imageResId.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", imageResId[i]);
            map.put("value", "value" + i);
            list.add(map);
        }
        return list;
    }

    /**
     * 取得最新产品
     * 
     * @param page
     *            页码
     */
    public List<Map<String, Object>> getNewProduct(int page) {

        // RequestParams params = new RequestParams();
        // params.put("page", String.valueOf(page));
        //
        // AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        // asyncHttpClient.get(Consts.URL_NEW_PRODUCT, params,
        // new MyAsyncHttpResponseHandler(context, view) {
        //
        // @Override
        // public void onSuccess(String json) {
        // super.onSuccess(json);
        //
        // }
        //
        // });

        int[] imageResId = new int[] { R.drawable.demo_main_product_image1, R.drawable.demo_main_product_image2,
                R.drawable.demo_main_product_image3, R.drawable.demo_main_product_image4,
                R.drawable.demo_main_product_image5, R.drawable.demo_main_product_image6 };
        String[] price = new String[] { "1502", "4646", "4642", "67864", "9871", "1432" };
        String[] intro = new String[] { "Proin luctus mi non adi piscing aliquset nibn eros ali",
                "Linsd jadf jue lisg adf pek", "Odf kgll ladf euif cnvi dfiwp", "Adfj perw qknx qern dliw dxnu",
                "Bdfi jbwp bmzl wpdf wenrd qdfm xptdk", "Gdfnu xynw ggiw bvow salu xmowa anxie" };
        String[] like = new String[] { "102", "446", "642", "784", "971", "132" };

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imageResId.length + 1; i++) {
            if (i == 0) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("image", R.drawable.demo_home_ad_small);
                list.add(map);

            } else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("image", imageResId[i - 1]);
                map.put("intro", intro[i - 1]);
                map.put("price", price[i - 1]);
                map.put("like", like[i - 1]);
                list.add(map);
            }
        }

        return list;
    }

    /**
     * 取得最热产品
     * 
     * @param page
     *            页码
     */
    public List<Map<String, Object>> getFireyProduct(int page) {

        // RequestParams params = new RequestParams();
        // params.put("page", String.valueOf(page));
        //
        // AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        // asyncHttpClient.post(Consts.URL_NEW_PRODUCT, params,
        // new MyAsyncHttpResponseHandler(context, view) {
        //
        // @Override
        // public void onSuccess(String json) {
        // super.onSuccess(json);
        //
        // }
        //
        // });

        int[] imageResId = new int[] { R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4, R.drawable.f5,
                R.drawable.f6 };
        String[] price = new String[] { "4518", "2373", "2132", "1999", "472", "994" };
        String[] intro = new String[] { "Nroin luctus mi non adi piscing aliquset nibn eros ali",
                "Linsd jadf jue lisg adf pek", "Odf kgll ladf euif cnvi dfiwp", "Adfj perw qknx qern dliw dxnu",
                "Bdfi jbwp bmzl wpdf wenrd qdfm xptdk", "Gdfnu xynw ggiw bvow salu xmowa anxie" };
        String[] like = new String[] { "876", "44", "712", "484", "101", "702" };

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imageResId.length + 1; i++) {
            if (i == 0) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("image", R.drawable.demo_home_ad_small);
                list.add(map);

            } else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("image", imageResId[i - 1]);
                map.put("intro", intro[i - 1]);
                map.put("price", price[i - 1]);
                map.put("like", like[i - 1]);
                list.add(map);
            }
        }

        return list;
    }

    /**
     * 取得产品详细数据
     * 
     * @param pid
     */
    public void getProductDetail(int pid) {

        RequestParams params = new RequestParams();
        params.put("pid", String.valueOf(pid));

        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(Consts.URL_PRODUCT_DETAIL, params, new MyAsyncHttpResponseHandler(context, view) {

            @Override
            public void onSuccess(String json) {
                super.onSuccess(json);

            }

        });
    }

    /**
     * 评论商品
     * 
     * @param pid
     *            商品id
     * @param content
     *            评论内容
     * @param username
     *            用户名
     * @param password
     *            密码
     */
    public void commentProduct(int pid, String content, String username, String password) {

        RequestParams params = new RequestParams();
        params.put("pid", String.valueOf(pid));
        params.put("content", content);
        params.put("username", username);
        params.put("password", password);

        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(Consts.URL_COMMENT_PRODUCT, params, new MyAsyncHttpResponseHandler(context, view) {

            @Override
            public void onSuccess(String json) {
                super.onSuccess(json);

            }

        });
    }

    /**
     * 商品添加到购物车
     * 
     * @param username
     *            用户名
     * @param password
     *            密码
     * @param pid
     *            商品id
     */
    public void productAddCard(String username, String password, int pid) {
        RequestParams params = new RequestParams();
        params.put("pid", String.valueOf(pid));
        params.put("username", username);
        params.put("password", password);

        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(Consts.URL_ADD_CARD, params, new MyAsyncHttpResponseHandler(context, view) {

            @Override
            public void onSuccess(String json) {
                super.onSuccess(json);

            }

        });

    }

    public List<Map<String, Object>> getShoppingCart() {
        int[] imageResId = new int[] { R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4, R.drawable.f5,
                R.drawable.f6 };
        String[] price = new String[] { "4518", "2373", "2132", "1999", "472", "994" };
        String[] name = new String[] { "Nroin luctus mi non adi piscing aliquset nibn eros ali",
                "Linsd jadf jue lisg adf pek", "Odf kgll ladf euif cnvi dfiwp", "Adfj perw qknx qern dliw dxnu",
                "Bdfi jbwp bmzl wpdf wenrd qdfm xptdk", "Gdfnu xynw ggiw bvow salu xmowa anxie" };

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imageResId.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", imageResId[i]);
            map.put("name", name[i]);
            map.put("price", price[i]);
            map.put("checkbox", false);
            list.add(map);
        }

        return list;
    }

    /**
     * 商品添加到喜欢
     * 
     * @param username
     *            用户名
     * @param password
     *            密码
     * @param pid
     *            商品id
     */
    public void doLikeProduct(String username, String password, int pid) {
        RequestParams params = new RequestParams();
        params.put("pid", String.valueOf(pid));
        params.put("username", username);
        params.put("password", password);

        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(Consts.URL_LIKE_PRODUCT, params, new MyAsyncHttpResponseHandler(context, view) {

            @Override
            public void onSuccess(String json) {
                super.onSuccess(json);

            }

        });
    }

    /**
     * 分享商品
     * 
     * @param username
     * @param password
     * @param pid
     */
    public void doShareProduct(String username, String password, int pid) {

    }

    /**
     * 修改密码
     * 
     * username 用户名 oldPassword 旧密码 newPassword1 新密码 newPassword2 重复密码
     */
    public void changePassword(String username, String oldPassword, String newPassword1, String newPassword2) {
        if (!newPassword1.equals(newPassword2)) {
            Toast.makeText(context, "两次密码输入不一样，请重新输入！", Toast.LENGTH_SHORT).show();
        } else {
            if (!oldPassword.equals(newPassword1)) {
                Toast.makeText(context, "修改密码成功！", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "新密码与旧密码相同，请重新输入！", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 切换用户
     */
    public void changeUser() {
        // 直接跳到登录页
    }

    /**
     * 提交反馈意见
     */
    public void feedback(String username, String password, String content, String email) {

    }

    public List<Map<String, Object>> getProductList(int Cid) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 10; i++) {
            if (i == 0) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("image", R.drawable.demo_home_ad_small);
                list.add(map);

            } else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("image", R.drawable.demo_main_product_image1);
                map.put("intro", "Proin luctus mi non adi piscing aliquset nibn eros ali");
                map.put("price", "250");
                map.put("like", "1839");
                list.add(map);
            }
        }

        return list;
    }

}
